import _imports  # 只在此项目有效，其他项目，请去掉此 import
import pybi as pbi
import pandas as pd
from src.data.dataset import load_df
from pathlib import Path
from template_charts import Template

charts = pbi.easy_echarts

df = load_df("某咖啡公司销售数据.xlsx")

data = pbi.set_source(df)


with pbi.gridBox(
    """
    title .
    sizebar card
    sizebar chart
"""
).set_columns_sizes(["1fr", "4fr"]).set_rows_sizes(["2em", "1fr", "3fr"]).set_style(
    "flex-grow:1"
):
    with pbi.flowBox():
        pbi.add_text("测试模板").set_style("")
        pbi.box().set_style("height:80%;border:1px solid black")
        pbi.add_text("2023").set_style("")

    with pbi.box().set_gridArea("sizebar"):
        pbi.add_checkbox(data["市场类别"]).set_itemDirection("column").set_classes(
            "checkbox-box"
        )
        pbi.add_checkbox(data["区域"]).set_itemDirection("column").set_classes(
            "checkbox-box"
        )
        pbi.add_checkbox(data["产品名称"]).set_classes("checkbox-box")

    opts = charts.make_bar(data, x="区域", y="销售额", agg="sum")
    tpl = Template(opts, "销售额", "区域")
    tpl.mainBox.set_gridArea("chart")

    with tpl._idx_box:
        with pbi.foreach(
            f"select 区域,sum(销售额)/1000 as value from {data} group by 区域 order by sum(销售额) desc"
        ) as row:
            tpl.add_index(f'{row["区域"]}', f"{row['value']}M")


pbi.to_html("test.html")
